package com.xiaomaoguai.springmvc.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * @Description :
 * @FileName: OpenTimeIntercepter.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年10月21日 下午2:22:38
 * @Version:V1.00
 */
public class OpenTimeIntercepter extends HandlerInterceptorAdapter {

	private final ThreadLocal<Long> threadLocal = new ThreadLocal<>();

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		threadLocal.set(new Date().getTime());
		return super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		Long long1 = threadLocal.get();
		request.setAttribute("time", new Date().getTime() - long1);
		super.postHandle(request, response, handler, modelAndView);
	}
}
